from paste.script.command import Command

from collections import namedtuple
import csv
import os
import paste

from cpswarehouse.config.environment import load_environment

from cpswarehouse.model import YearDimension, GradeDimension, RaceDimension, RecordFact, SchoolDimension
from cpswarehouse.model.meta import Session as session


RaceDataRecord = namedtuple('RaceDataRecord', 'yr,unit,school,grade,total,white_total,white_pct,black_total,black_pct,nat_amer_tot,ant_amer_pct,asian_total,asian_pct,hispanic_total,hispanic_pct,multi_race_total,multi_race_pct,mexican_total,mexcian_pct,puerto_rican_total,puerto_rican_pct,cuban_total,cuban_pct,other_hispanic_total,other_hispanic_pct,multi_hispanic_total,multi_hispanic_pct')        

def race_data_records(filename):
    for record in map(RaceDataRecord._make, csv.reader(open(filename, "rb"))):
        yield record


class CpsImport(Command):
    # Parser configuration
    summary = "Import the raw data from the CPS Racial/Ethnic Survey CSV."
    usage = "<settings_ini> <cps_datafile>"
    group_name = "cpswarehouse"
    parser = Command.standard_parser(verbose=False)

    def command(self):
        #make sure the config file arg is set
        if(len(self.args) != 2):
            print self.parser.format_help()
            return
        
        #load the environment
        conf_filename = os.path.abspath(self.args[0])
        conf = paste.deploy.appconfig("config:%s" % conf_filename)
        load_environment(conf.global_conf, conf.local_conf)

        data_filename = os.path.abspath(self.args[1])
        
        #should take this from arguments
        records = race_data_records(data_filename)
        records.next() #advance past header

        for record in records:
            try:
                year = YearDimension.get_instance(record.yr)
                grade = GradeDimension.get_instance(record.grade)
                race = RaceDimension.get_instance(nativeamerican=record.nat_amer_tot,
                                                  asian=record.asian_total,
                                                  black=record.black_total,
                                                  hispanic=record.hispanic_total,
                                                  multiracial=record.multi_race_total,
                                                  white=record.white_total)
                school = SchoolDimension.get_instance(record.school, record.unit)

                session.add(RecordFact(year, grade, race, school))
            except Exception as e:
                print(e)



        print "Imported %s YearDimensions." % len(YearDimension.items)
        for year in YearDimension.items.values():
            session.add(year)

        print "Imported %s GradeDimensions." % len(GradeDimension.items)
        for grade in GradeDimension.items.values():
            session.add(grade)

        print "Imported %s RaceDimensions." % len(RaceDimension.items)
        for race in RaceDimension.items.values():
            session.add(race)

        print "Imported %s SchoolDimensions." % len(SchoolDimension.items)
        for school in SchoolDimension.items.values():
            session.add(school)
        session.commit()
